<?php

namespace app\adminmini\service;

use app\adminmini\model\Room;
use app\common\library\wechat\WxQrcode;
use think\facade\Db;

class RoomService extends BaseService
{

    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new Room();
    }



    public function updateUserInfo()
    {

        // 参数
        $param = request()->param();
        if (isset($param['lat'])) {
            if ($param['lat'] != "") {
                $lan = explode(',', $param['lat']);
            } else {
                $lan[0] = '';
                $lan[1] = '';
            }


        } else {
            $lan[0] = '';
            $lan[1] = '';
        }


        $data = [
            'head_url' => $param['head_url'],
            'room_name' => $param['room_name'],
            'mobile' => $param['mobile'],
            'make_image' => $param['make_image'],
            'name' => $param['name'],
            'address' => $param['address'],
            'status' => $param['status'],
            'hours_add_time' => $param['hours_add_time'],
            'hours_end_time' => $param['hours_end_time'],
            'hours_add_time_fen' => $param['hours_add_time_fen'],
            'hours_end_time_fen' => $param['hours_end_time_fen'],
            'latitude' => $lan[0],
            'longitude' => $lan[1],
            'time' => $param['time'],
        ];
        $cover = getter($data, "head_url");
        if (!empty($cover)) {
            if (strpos($cover, "temp") !== false) {
                $data['head_url'] = save_image($cover, 'article');
            } else {
                $data['head_url'] = str_replace(IMG_URL, "", $cover);
            }
        }
        // 文章图集
        $imgStr = [];
        $imgsList = isset($data['make_image']) ? $data['make_image'] : [];
        if ($imgsList) {
            foreach ($imgsList as $val) {
                if (strpos($val, "temp")) {
                    //新上传图片
                    $imgStr[] = save_image($val, 'article');
                } else {
                    //过滤已上传图片
                    $imgStr[] = str_replace(IMG_URL, "", $val);
                }
            }
            $data['make_image'] = serialize($imgStr);
        }
        // 城市数据处理
        $city = isset($param['city']) ? $param['city'] : [];
        if (!empty($city)) {
            $data['province_code'] = $city[0];
            $data['city_code'] = $city[1];
            if (isset($city[2])) {
                $data['district_code'] = $city[2];
            } else {
                $data['district_code'] = '';
            }
            unset($param['city']);
        } else {
            $data['province_code'] = 0;
            $data['city_code'] = 0;
            $data['district_code'] = 0;
            unset($param['city']);
        }
        $info = Db::name('mini_room_list')->where('id', '=', $this->userId())->find();
        if ($info['code_image_url'] == "") {
            $data['id'] = $info['id'];
            $data['code_image_url'] = 'http://imageqlx.qishenghui.net' . ('/' . $this->openCode($data));
        }
        $data['update_time'] = time();
        $result = Db::table('evt_mini_room_list')->where('id', '=', $info['id'])->update($data);
        if (!$result) {
            return message("更新资料信息失败", false);
        }
        cache('room',false);
        return message("更新资料信息成功");
    }

    /**
     * 获取用户信息
     * @param $userId 用户ID
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 牧羊人
     * @since 2020/11/14
     */
    public function getUserInfo()
    {


        $info = Db::name('mini_room_list')->where('id', '=', $this->userId())->find();
        $city[] = !empty($info['province_code']) ? strval($info['province_code']) : "";
        $city[] = !empty($info['city_code']) ? strval($info['city_code']) : "";
        $city[] = !empty($info['district_code']) ? strval($info['district_code']) : "";
        $info['city'] = $city;
        if (isset($info['head_url'])) {
            if ($info['head_url'] != "") {
                $info['head_url'] = get_image_url($info['head_url']);
            }
        }

        if ($info['province_code']) {
            $info['province_name'] = Db::name('city')->where('citycode', $info['province_code'])->find();
        }
        if ($info['city_code']) {
            $info['city_name'] = Db::name('city')->where('citycode', $info['city_code'])->find();
        }
        if ($info['district_code'] > 0) {

            $info['district_name'] = Db::name('city')->where('citycode', $info['district_code'])->find();

            if (!isset($infolist['district_name']['name'])) {
                $info['district_name']['name'] = '';
            }


        } else {
            $info['district_name']['name'] = '';
        }
        $info['address_lan'] = $info['province_name']['name'] . $info['city_name']['name'] . $info['district_name']['name'] . $info['address'];


        if (isset($info['make_image']) && $info['make_image']) {

            $imgsList = unserialize($info['make_image']);
            foreach ($imgsList as &$val) {
                $val = get_image_url($val);

            }
            $info['make_image'] = $imgsList;

        }
        $info['lat'] = $info['latitude'] . ',' . $info['longitude'];
        return message("操作成功", true, $info);
    }


    public function openCode($data)
    {
        $id = $data['id'];


        $qrCode = new WxQrcode([
            'app_id' => 'wxaa56fddbce4f095f',
            'app_secret' => '974328720502819e4849956cfb829019',
        ]);
        $img = $id . '_' . $data['id'] . '.png';
        $file_path = 'code/' . $data['id'] . '/';
        $path = root_path() . 'public/uploads/' . $file_path;
        if (!file_exists($path)) {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($path, 0777, true);
        }
        $filename = $path . $img;
        $info = $qrCode->getQrcode($filename, '&c=' . $id, 'pages/index/room/index');
        return ($file_path . $img);
    }
}